package chart;

import java.awt.BorderLayout;

import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.BorderFactory;
import javax.swing.border.BevelBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import java.awt.Rectangle;
import java.awt.Point;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Color;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.JButton;

import newclasses.Recovery;

import org.jfree.ui.RefineryUtilities;

import temp.TypeConverter;
import teste.ReadSound;

public class MainFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel jGraficoPanel = null;
	private JPanel jMenuPanel = null;
	private JLabel jMenuLabel = null;
	private JLabel jTamanhoJanelaLabel = null;
	private JLabel jQtdePacotesPerdidos = null;
	private JSlider jTamanhoJanelaSlider = null;
	private JLabel jTamanhoTemplateLabel = null;
	private JLabel jQtdePacotesLabel = null;
	private JSlider jTamTemplateSlider = null;
	private JSlider jQtdePacoteSlider = null;
	private JSlider jQtdePerdaSlider = null;
	private JButton jPLayButton = null;
	private JFileChooser caminhoArquivo;
	private Recovery r;  //  @jve:decl-index=0:
	private int tamJanela, tamTemplate, qtdepacotes,qtdPerda;
	
	
	
	private Vector<Short> sound;  //  @jve:decl-index=0:
	
	private Vector<Short> sound_recovered;
	

	/**
	 * This is the default constructor
	 */
	public MainFrame() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(new Dimension(1280, 770));
		this.setLocation(new Point(0, 0));
		this.setResizable(false);
		this.setContentPane(getJContentPane());
		this.setTitle("Substituicao Onda");
		this.setVisible(true);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
			jContentPane.add(getJGraficoPanel(), null);
			jContentPane.add(getJMenuPanel(), null);
		}
		return jContentPane;
	}
	
	/**
	 * This method initializes jGraficoPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJGraficoPanel() {
		if (jGraficoPanel == null) {
			jGraficoPanel = new JPanel();
			jGraficoPanel.setLayout(null);
			jGraficoPanel.setBounds(new Rectangle(24, 25, 753, 688));
			jGraficoPanel.setPreferredSize(new Dimension(753,688));
			JPanel grafico = getGrafico();
			grafico.setVisible(true);
			grafico.setBorder(jGraficoPanel.getBorder());
			jGraficoPanel.add(grafico);
			
		}
		return jGraficoPanel;
	}
	
	public void refazerGrafico(){
		
		jGraficoPanel.removeAll();
		JPanel grafico = getGrafico();
		grafico.setBorder(jGraficoPanel.getBorder());
		grafico.setVisible(true);
		jGraficoPanel.add(grafico);
		jGraficoPanel.repaint();
		jGraficoPanel.setVisible(true);
	}
	
	
	public JPanel getGrafico(){
		
		if(r != null){ 
			
			XYAreaChartDemo2 chart = new XYAreaChartDemo2("Waveform Substitution Algorithm",sound_recovered,sound,"Recovered","Original");
			chart.pack();
			chart.setSize(753, 688);
			JPanel painel = (JPanel)chart.getContentPane();
			painel.setVisible(true);
	
		return painel;
		}
		return new JPanel();
	}

	/**
	 * This method initializes jMenuPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJMenuPanel() {
		if (jMenuPanel == null) {
			jQtdePacotesLabel = new JLabel();
			jQtdePacotesLabel.setBounds(new Rectangle(15, 282, 228, 36));
			jQtdePacotesLabel.setText("Tamanho do Pacote em Milisegundos");
			jTamanhoTemplateLabel = new JLabel();
			jTamanhoTemplateLabel.setBounds(new Rectangle(13, 164, 250, 36));
			jTamanhoTemplateLabel.setText("Tamanho do Template em bytes");
			jTamanhoJanelaLabel = new JLabel();
			jTamanhoJanelaLabel.setBounds(new Rectangle(16, 65, 220, 29));
			jTamanhoJanelaLabel.setText("Tamanho da Janela em pacotes");
			
			jQtdePacotesPerdidos = new JLabel();
			jQtdePacotesPerdidos.setBounds(new Rectangle(16,420, 220, 29));
			jQtdePacotesPerdidos.setText("Quantidade de  Pacotes Perdidos.");
			
			
			jMenuLabel = new JLabel();
			jMenuLabel.setBounds(new Rectangle(166, 18, 104, 29));
			jMenuLabel.setText("Menu");
			jMenuPanel = new JPanel();
			jMenuPanel.setLayout(null);
			jMenuPanel.setBounds(new Rectangle(804, 25, 447, 686));
			jMenuPanel.setBorder(BorderFactory.createLineBorder(Color.blue, 2));
			jMenuPanel.add(jMenuLabel, null);
			jMenuPanel.add(jTamanhoJanelaLabel, null);
			jMenuPanel.add(jQtdePacotesPerdidos, null);
			jMenuPanel.add(getJTamanhoJanelaSlider(), null);
			jMenuPanel.add(jTamanhoTemplateLabel, null);
			jMenuPanel.add(jQtdePacotesLabel, null);
			jMenuPanel.add(getJTamTemplateSlider(), null);
			jMenuPanel.add(getJQtdePacoteSlider(), null);
			jMenuPanel.add(getQtdPerdaJSlider(), null);
			jMenuPanel.add(getJPLayButton(), null);
		}
		return jMenuPanel;
	}

	/**
	 * This method initializes jTamanhoJanelaSlider	
	 * 	
	 * @return javax.swing.JSlider	
	 */
	private JSlider getJTamanhoJanelaSlider() {
		if (jTamanhoJanelaSlider == null) {
			jTamanhoJanelaSlider = new JSlider();
			jTamanhoJanelaSlider.setBounds(new Rectangle(93, 100, 207, 48));
			jTamanhoJanelaSlider.setMaximum(10);
			jTamanhoJanelaSlider.setMinimum(5);
			jTamanhoJanelaSlider.setMinorTickSpacing(1);
			jTamanhoJanelaSlider.setPaintLabels(true);
			jTamanhoJanelaSlider.setPaintTicks(true);
			jTamanhoJanelaSlider.setSnapToTicks(true);
			jTamanhoJanelaSlider.setMajorTickSpacing(1);
			jTamanhoJanelaSlider
					.addChangeListener(new ChangeListener() {
				        public void stateChanged(ChangeEvent evt) {
				            JSlider slider = (JSlider)evt.getSource();
				    
				            if (!slider.getValueIsAdjusting()) {
				            	tamJanela = jTamanhoJanelaSlider.getValue();
				            }
				        }
				    });
		}
		return jTamanhoJanelaSlider;
	}

	/**
	 * This method initializes jTamTemplateSlider	
	 * 	
	 * @return javax.swing.JSlider	
	 */
	private JSlider getJTamTemplateSlider() {
		if (jTamTemplateSlider == null) {
			jTamTemplateSlider = new JSlider();
			jTamTemplateSlider.setBounds(new Rectangle(28, 214, 295, 45));
			jTamTemplateSlider.setMaximum(100);
			jTamTemplateSlider.setMinimum(20);
			jTamTemplateSlider.setMinorTickSpacing(5);
			jTamTemplateSlider.setPaintLabels(true);
			jTamTemplateSlider.setPaintTicks(true);
			jTamTemplateSlider.setSnapToTicks(true);
			jTamTemplateSlider.setMajorTickSpacing(10);
			jTamTemplateSlider
					.addChangeListener(new ChangeListener() {
				        public void stateChanged(ChangeEvent evt) {
				            JSlider slider = (JSlider)evt.getSource();
				    
				            if (!slider.getValueIsAdjusting()) {
				            	tamTemplate = jTamTemplateSlider.getValue();
				            }
				        }
				    });
		}
		return jTamTemplateSlider;
	}

	/**
	 * This method initializes jQtdePacoteSlider	
	 * 	
	 * @return javax.swing.JSlider	
	 */
	private JSlider getJQtdePacoteSlider() {
		if (jQtdePacoteSlider == null) {
			jQtdePacoteSlider = new JSlider();
			jQtdePacoteSlider.setBounds(new Rectangle(28, 363, 393, 41));
			jQtdePacoteSlider.setMaximum(200);
			jQtdePacoteSlider.setMinimum(50);
			jQtdePacoteSlider.setMinorTickSpacing(10);
			jQtdePacoteSlider.setPaintLabels(true);
			jQtdePacoteSlider.setPaintTicks(true);
			jQtdePacoteSlider.setSnapToTicks(true);
			jQtdePacoteSlider.setMajorTickSpacing(20);
			jQtdePacoteSlider
					.addChangeListener(new ChangeListener() {
				        public void stateChanged(ChangeEvent evt) {
				            JSlider slider = (JSlider)evt.getSource();
				    
				            if (!slider.getValueIsAdjusting()) {
				            	qtdepacotes = jQtdePacoteSlider.getValue();
				            }
				        }
				    });
		}
		return jQtdePacoteSlider;
	}

	
	
	private JSlider getQtdPerdaJSlider() {
		if (jQtdePerdaSlider == null) {
			jQtdePerdaSlider = new JSlider();
			jQtdePerdaSlider.setBounds(new Rectangle(28, 460, 393, 41));
			jQtdePerdaSlider.setMaximum(50);
			jQtdePerdaSlider.setMinimum(10);
			jQtdePerdaSlider.setMinorTickSpacing(1);
			jQtdePerdaSlider.setPaintLabels(true);
			jQtdePerdaSlider.setPaintTicks(true);
			jQtdePerdaSlider.setSnapToTicks(true);
			jQtdePerdaSlider.setMajorTickSpacing(10);	
			jQtdePerdaSlider.addChangeListener(new ChangeListener() {
		        public void stateChanged(ChangeEvent evt) {
		            JSlider slider = (JSlider)evt.getSource();
		    
		            if (!slider.getValueIsAdjusting()) {
		            	qtdPerda = jQtdePerdaSlider.getValue();
		            }
		        }
		    });

		}
		return jQtdePerdaSlider;
	}
	/**
	 * This method initializes jPLayButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJPLayButton() {
		if (jPLayButton == null) {
			jPLayButton = new JButton();
			jPLayButton.setBounds(new Rectangle(103, 574, 183, 61));
			jPLayButton.setText("PLAY");
			jPLayButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
						play();
				}
			});
		}
		return jPLayButton;
	}
	
	public void play(){
		
		if (qtdepacotes == 0 || tamJanela == 0 || tamTemplate== 0 || qtdPerda==0) {
			qtdepacotes = 100;
			tamJanela = 20;
			tamTemplate = 10;
			qtdPerda = 10;
		}
		
		r = new Recovery(qtdepacotes,tamJanela,tamTemplate);
		
		r.read("wave1.wav");
		
		Vector<Byte> soundInBytes = r.getOriginalFile();
		
		
		r.simulateLoss(qtdPerda);
		
		Vector<Byte> soundRecoveredInBytes = r.recovery();

		
		Iterator<Byte> iterator = soundInBytes.iterator();
		Iterator<Byte> iteratorRecovered = soundRecoveredInBytes.iterator();
		
		sound = new Vector<Short>();
		sound_recovered = new Vector<Short>();
		
		while(iterator.hasNext() && iteratorRecovered.hasNext()){
			sound.add(TypeConverter.byteSignedToUnsegned(iterator.next()));
			sound_recovered.add(TypeConverter.byteSignedToUnsegned(iteratorRecovered.next()));
		}

		refazerGrafico();
		
		r.play(soundRecoveredInBytes);
	}

	public static void main(String[] args) {
		MainFrame mf = new MainFrame();
	}
	

}
